package com.pancm.datacollector.config;

import com.pancm.datacollector.collector.Collector;
import com.pancm.datacollector.collector.CollectorConfig;
import com.pancm.datacollector.collector.impl.HttpCollector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.beans.factory.annotation.Value;

import java.util.HashMap;

@Configuration
public class CollectorConfiguration {
    
    @Autowired
    private ConfigurationManager configurationManager;
    
    @Value("${collector.type:http}")  // 默认使用http类型
    private String collectorType;
    
    @Bean
    public Collector dataCollector() {  // 改为更通用的名称
        switch(collectorType.toLowerCase()) {
            case "http":
                return createHttpCollector();
            // 后续可以添加其他类型的采集器
            // case "tcp":
            //     return createTcpCollector();
            // case "mqtt":
            //     return createMqttCollector();
            default:
                throw new IllegalArgumentException("Unsupported collector type: " + collectorType);
        }
    }
    
    /**
     * 创建HTTP采集器
     */
    private HttpCollector createHttpCollector() {
        // 从配置文件中读取配置信息
        CollectorConfig config = new CollectorConfig();
        config.setCollectInterval(configurationManager.getIntegerValue("collector.http.collectInterval"));
        config.setProtocol(configurationManager.getStringValue("collector.http.protocol"));
        config.setServerHost(configurationManager.getStringValue("collector.http.serverHost"));
        config.setServerPort(configurationManager.getIntegerValue("collector.http.serverPort"));
        
        // 设置参数
        HashMap<String, Object> parameters = new HashMap<>();
        parameters.put("url", configurationManager.getStringValue("collector.http.parameters.url"));
        parameters.put("method", configurationManager.getStringValue("collector.http.parameters.method"));
        
        // 设置请求头
        HashMap<String, String> headers = new HashMap<>();
        headers.put("Content-Type", configurationManager.getStringValue("collector.http.parameters.headers.Content-Type"));
        headers.put("Authorization", configurationManager.getStringValue("collector.http.parameters.headers.Authorization"));
        parameters.put("headers", headers);
        
        config.setParameters(parameters);
        
        return new HttpCollector(config);
    }
} 